package top.xiangqifu.springboot2.user.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;

@Data
public class PageBean implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final long MIN_SIZE = 1;
    public static final long MAX_SIZE = 1000;
    public static final int DEFAULT_PAGE = 1;
    public static final int DEFAULT_SIZE = 10;

    private long pageNumber = DEFAULT_PAGE;
    private long pageSize = DEFAULT_SIZE;
    private long total = 0;
    private long pageCount = 0;

    public void setPageNumber(long pageNumber) {
        if (pageNumber >= DEFAULT_PAGE) {
            this.pageNumber = pageNumber;
        }
    }

    public void setPageSize(long pageSize) {
        if (pageSize < MIN_SIZE || pageSize > MAX_SIZE) {
            return;
        }
        this.pageSize = pageSize;
    }

    public PageBean buildPageInfo(IPage<?> page) {
        if (page == null) {
            return this;
        }
        this.setTotal(page.getTotal());
        this.setPageCount(page.getPages());
        return this;
    }
    public static <T> IPage<T> buildPageInfo(PageBean pageBean, Class<T> tclass) {
        IPage<T> page = new Page<>();
        page.setSize(pageBean.getPageSize());
        page.setCurrent(pageBean.getPageNumber());
        return page;
    }

    public void setPageInfo(PageInfo<?> pageInfo) {
        this.setTotal((int)pageInfo.getTotal());
        this.setPageNumber(pageInfo.getPageNum());
    }

    public PageBean populate(IPage<?> page) {
        this.setTotal(page.getTotal());
        this.setPageCount(page.getPages());
        return this;
    }

    public <T> IPage<T> toPage(Class<T> clazz){
        return new Page<>(this.pageNumber, this.pageSize);
    }

}
